package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.User;
import com.jt.service.DubboCartService;
import com.jt.util.UserThreadLocal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Reference(check = false)
    private DubboCartService cartService;

    /**
     * url: http://www.jt.com/cart/show.html
     * 返回值: 购物车列表页面 cart.jsp
     * 页面数据取值:  ${cartList}
     * request对象的取值,只能在Controller层中取值....
     */
    @RequestMapping("/show")
    public String cartShow(Model model, HttpServletRequest request){
        //1.获取用户信息 ticket  redis  userJSON user对象 userId
        User user = (User) request.getAttribute("JT_USER");
        long userId = user.getId();
        List<Cart> cartList = cartService.findCartListByUserId(userId);
        model.addAttribute("cartList",cartList);
        return "cart";
    }

    /**
     * 实现商品数量的更新操作
     * url地址:   http://www.jt.com/cart/update/num/1474391990/16
     * 参数:      itemId/num
     * 返回值:    void
     */
    @RequestMapping("/update/num/{itemId}/{num}")
    @ResponseBody   //1.返回值JSON  2.结束ajax请求
    public void updateNum(Cart cart){

        long userId = UserThreadLocal.get().getId();
        cart.setUserId(userId);
        cartService.updateNum(cart);
    }

    /**
     * 完成购物车新增操作
     * URL地址:   http://www.jt.com/cart/add/562379.html
     * 参数:      整个form表单
     * 返回值:    重定向到购物车列表页面
     */
    @RequestMapping("/add/{itemId}")
    public String addCart(Cart cart){
        long userId = UserThreadLocal.get().getId();
        cart.setUserId(userId);
        cartService.addCart(cart);
        return "redirect:/cart/show.html";
    }

    /**
     * 删除购物车Controller
     * URL地址: http://www.jt.com/cart/delete/1474391990.html
     * 返回值:  重定向到购物车列表页面
     */
    @RequestMapping("/delete/{itemId}")
    public String deleteCarts(Cart cart){

        long userId = UserThreadLocal.get().getId();
        cart.setUserId(userId);
        cartService.deleteCart(cart);
        return "redirect:/cart/show.html";
    }




}
